﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Fishing.Dal;
using Fishing.Model;

namespace Fishing.Admin
{
    public partial class Permission : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                // 循环读出所有节点  -- 目前只有两个大块

                var _dal = new SSEnumDal();
                var allEnum = _dal.GetSSEnum();

                // 钓船审批  (1	0	1	1	钓船审批)
                var boatPermission = allEnum.Where(a => a.enum_Parent_ID == 1).OrderBy(a => a.enum_Seq);
                lvBoatPermission.Items.Clear();
                lvBoatPermission.DataSource = boatPermission;
                lvBoatPermission.DataTextField = "enumName";
                lvBoatPermission.DataValueField = "enumID";
                lvBoatPermission.DataBind();

                // 系统管理  (2	0	2	2	钓船审批)
                var sysPermission = allEnum.Where(a => a.enum_Parent_ID == 2).OrderBy(a => a.enum_Seq);
                lvSysPermission.Items.Clear();
                lvSysPermission.DataSource = sysPermission;
                lvSysPermission.DataTextField = "enumName";
                lvSysPermission.DataValueField = "enumID";
                lvSysPermission.DataBind();


                BindData();
            }


        }


        public void BindData()
        {
            var str = "";
            if (txtSerchWords.Text.Length > 0)
            {
                str = " and roleName like '%" + txtSerchWords.Text + "%'";
            }

            var _dal = new AdminRoleDal();
            var alldata = _dal.GetRoleDataBy(str);

            tvMenu.Nodes.Clear();
            // 首先绑定子节点
            foreach (var adminRole in alldata)
            {
                tvMenu.Nodes.Add(new TreeNode(adminRole.roleName, adminRole.roleID));
                //如果存在子节点
            }

            foreach (ListItem li in lvBoatPermission.Items)
            {
                li.Selected = false;
            }
            foreach (ListItem li in lvSysPermission.Items)
            {
                li.Selected = false;
            }
        }

        protected void ImgBtnCan_Click(object sender, ImageClickEventArgs e)
        {
            txtSerchWords.Text = "";
            BindData();
            
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ImgBtnSave_Click(object sender, ImageClickEventArgs e)
        {
            var loginuser = "";
            if (Session["UserID"] != null)
            {
                loginuser = Session["UserID"].ToString();
            }

            if (string.IsNullOrEmpty(txtroleID.Value))
            {
                ClientScript.RegisterStartupScript(this.GetType(), "提示",
                    "<script type='text/javascript'>alert('请选择角色！');</script>");
                return;
            }

            var permissionids = "";
            foreach (ListItem item in lvBoatPermission.Items)
            {
                if (item.Selected)
                {
                    permissionids = permissionids + item.Value + ',';
                }
            }
            foreach (ListItem item in lvSysPermission.Items)
            {
                if (item.Selected)
                {
                    permissionids = permissionids + item.Value + ',';
                }
            }

            if (permissionids.Length > 0)
            {

                permissionids = permissionids.Remove(permissionids.Length - 1);

                var _dal = new AdminRolePermissionDal();
                var model = new admin_role_permission()
                {
                    rolePermissionID = Guid.NewGuid().ToString().Substring(24, 10),
                    roleID = tvMenu.SelectedValue,
                    enumID = permissionids
                };




                // 提交
                if (!_dal.IsExists(txtroleID.Value))
                {

                    if (_dal.InsertDate(model))
                    {
                        //添加成功
                        ClientScript.RegisterStartupScript(this.GetType(), "提示",
                            "<script type='text/javascript'>alert('添加成功！');</script>");
                        txtroleID.Value = model.roleID;
                        BindData();
                    }
                    else
                    {
                        // 添加失败
                        ClientScript.RegisterStartupScript(this.GetType(), "提示",
                            "<script type='text/javascript'>alert('添加失败！');</script>");
                    }
                }

                else
                {
                    if (_dal.UpdateDate(model))
                    {
                        //修改成功
                        ClientScript.RegisterStartupScript(this.GetType(), "提示",
                            "<script type='text/javascript'>alert('修改成功！');</script>");
                        BindData();
                    }
                    else
                    {
                        // 修改失败
                        ClientScript.RegisterStartupScript(this.GetType(), "提示",
                            "<script type='text/javascript'>alert('修改失败！');</script>");
                    }
                }
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "提示",
                            "<script type='text/javascript'>alert('请选择权限！');</script>");
            }
        }

        /// <summary>
        /// 搜索
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ImgbtnSelect_Click(object sender, ImageClickEventArgs e)
        {
            BindData();
        }


        protected void tvMenu_SelectedNodeChanged(object sender, EventArgs e)
        {
            var role = this.tvMenu.SelectedNode.Value;
            if (role.Length > 0)
            {
                var str = " and roleid='" + role + "'";

                var _dal = new AdminRolePermissionDal();
                var alldata = _dal.GetDataList(str);

                foreach (ListItem li in lvBoatPermission.Items)
                {
                    li.Selected = false;
                }
                foreach (ListItem li in lvSysPermission.Items)
                {
                    li.Selected = false;
                }

                if (alldata.Count > 0)
                {
                    foreach (var enumId in alldata.First().enumID.Split(','))
                    {
                        // 获取所有权限之后循环选中
                        if (lvBoatPermission.Items.FindByValue(enumId) != null)
                            lvBoatPermission.Items.FindByValue(enumId).Selected = true;

                        if (lvSysPermission.Items.FindByValue(enumId) != null)
                            lvSysPermission.Items.FindByValue(enumId).Selected = true;
                    }
                }


                txtroleID.Value = role;
            }

        }
    }
}